<?php

class FormElement_Images extends FormElement
{

  public $foreignKey;

  public $table;

  public function beforeIndex()
  {
    if (Simplify::request()->get('mode') == 'upload_check') {
      $data = array();
      echo json_encode($data);
      exit();
    }
    elseif (Simplify::request()->get('mode') == 'upload') {
      $data = array();

      if (! empty($_FILES)) {
        try {
          $upload = Component::factory('upload');
          $upload->getParameters()->set('hash_filename', true);
          $upload->upload('Filedata');
        }
        catch (Exception $e) {
          $data['error'] = $e->getMessage();
        }
      }

      echo $this->render($row, $action);//json_encode($data);
      exit();
    }
  }

  /*public function render($row, $action)
  {
    return $output;
  }*/

  public function getTable()
  {
    if (empty($this->table)) {
      $this->table = $this->form->getTable();
    }

    return $this->table;
  }

  public function getKeyField()
  {
    if (empty($this->keyField)) {
      $this->keyField = $this->getField();
    }

    return $this->keyField;
  }

  public function getLabelField()
  {
    if (empty($this->labelField)) {
      $this->labelField = $this->getField();
    }

    return $this->labelField;
  }

  public function getOptions()
  {
    if (is_null($this->options)) {
      if (empty($this->query)) {
        $this->query = DB::getInstance()->query()->from($this->getTable())->select($this->getKeyField(), $this->getLabelField());
      }
      elseif (is_string($this->query)) {
        $this->query = DB::getInstance()->query($this->query);
      }

      $this->query->setParams($this->queryParams);

      $result = $this->query->execute($this->queryData);

      $data = $result->fetchAll();

      $options = sy_array_to_options($data, $this->getKeyField(), $this->getLabelField());

      $options = $this->filters('options', $options, $data);

      $this->options = $options;
    }

    return $this->options;
  }

}

?>